//
//  YWToningController.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/20.
//

#import "YWToningController.h"
#import "YWToningView.h"

@interface YWToningController ()

- (IBAction)dismissClick:(UIButton *)sender;
- (IBAction)conformBtnClick:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIView *handleView;

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) YWToningView *drawView;

@property(nonatomic,assign)BOOL isAlreadyAppear;


@property (weak, nonatomic) IBOutlet UISlider *redSlider;

@property (weak, nonatomic) IBOutlet UISlider *greenSlider;

@property (weak, nonatomic) IBOutlet UISlider *blueSlider;
- (IBAction)redSliderChange:(UISlider *)sender;
- (IBAction)greenSliderChange:(UISlider *)sender;
- (IBAction)blueSliderChange:(UISlider *)sender;

@end

@implementation YWToningController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    //给imageView原图
    self.imageView.image = self.image;
    
    
    //创建画view并添加到bgView
    YWToningView *drawView = [[YWToningView alloc] init];
    self.drawView = drawView;
    [self.bgView addSubview:drawView];
}

//初始化UI
-(void)setupUI{
    
    self.view.alpha = 0;
    self.handleView.transform = CGAffineTransformMakeTranslation(0, 300);
    
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.4 animations:^{
        
        self.view.alpha = 1;
        
        self.handleView.transform = CGAffineTransformIdentity;
    }];
    
    
    //显示第一张底图层
    if (self.isAlreadyAppear == NO){
        
        self.isAlreadyAppear = YES;
        
        self.imageView.hidden = YES; //不是对比的话一开始imageView就要隐藏
        
        MJWeakSelf
        
        CGFloat w = self.bgView.frame.size.width;
        CGFloat h = w * self.image.size.height*1.0/self.image.size.width;
        CGFloat y = 0;
        CGFloat x = 0;
        if (h > self.bgView.frame.size.height) {
            h = self.bgView.frame.size.height;
            y = 0;
            w = h * self.image.size.width*1.0/self.image.size.height;
            x = (self.bgView.frame.size.width - w)*1.0/2;
            
            [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(weakSelf.drawView.superview).with.offset(x);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(-x);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.top.equalTo(weakSelf.drawView.superview).with.offset(0);
            }];
            
        }
        else{
            x = 0;
            w = self.bgView.frame.size.width;
            h = w * self.image.size.height*1.0/self.image.size.width;
            y = (self.bgView.frame.size.height - h)*0.5;
            
            
            [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(0);
                make.top.equalTo(weakSelf.drawView.superview).with.offset(y);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(-y);
            }];
            
        }
        
        //设置第一张原始图片
        [self.drawView setFirstImage:self.image];
    }
    
    
    
}
-(void)dismiss{
    [UIView animateWithDuration:0.4 animations:^{
        
        self.handleView.transform = CGAffineTransformMakeTranslation(0, 300);
        self.view.alpha = 0;
    }completion:^(BOOL finished) {
        
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (IBAction)dismissClick:(UIButton *)sender{
    [self dismiss];
    
}
- (IBAction)conformBtnClick:(UIButton *)sender{
    
    if([self.delegate respondsToSelector:@selector(toningController:newImage:)]){
        
        [self.delegate toningController:self newImage:[self.drawView getCurrentLayer]];
        
    }
    
    [self dismiss];
}

- (IBAction)blueSliderChange:(UISlider *)sender {
    [self.drawView setBlueColor:sender.value];
}

- (IBAction)greenSliderChange:(UISlider *)sender {
    [self.drawView setGreenColor:sender.value];
}

- (IBAction)redSliderChange:(UISlider *)sender {
    [self.drawView setRedColor:sender.value];
}
@end
